package com.example.composedemo

import android.graphics.Typeface
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.TweenSpec
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun CustomTheme(
    mainViewModel: MainViewModel,
    content: @Composable () -> Unit
) {
    /**
     * 自定义主题色值
     */
    val targetColors: AppColors
    if (isSystemInDarkTheme()) {
        //如果是深色模式，则只能是深色模式的色值组，无法切换
        targetColors = DarkColors
    } else {
        targetColors = when (mainViewModel.chosenThemeId.value) {
            ThemeKinds.RED.name -> {
                RedThemeColors
            }
            ThemeKinds.YELLOW.name -> {
                YellowThemeColors
            }
            ThemeKinds.BLUE.name -> {
                BlueThemeColors
            }
            else -> {
                DefaultColors
            }
        }
    }
    //渐变实现
    mainViewModel.primaryColor = animateColorAsState(targetColors.primary, TweenSpec(500)).value
    mainViewModel.backgroundColor =
        animateColorAsState(targetColors.background, TweenSpec(500)).value

    /**
     * 自定义主题的 h4 文案大小
     */
    val targetTextSizes: AppTextSizes = when (mainViewModel.chosenThemeId.value) {
        ThemeKinds.RED.name -> {
            RedTextSizes
        }
        ThemeKinds.YELLOW.name -> {
            YellowTextSizes
        }
        ThemeKinds.BLUE.name -> {
            BlueTextSizes
        }
        else -> {
            DefaultTextSizes
        }
    }
    mainViewModel.h4TextSize = animateFloatAsState(targetTextSizes.h4_size, TweenSpec(500)).value

    /**
     * 自定义主题的 h4 文案字体
     */
    val defaultTextStyle = AppTextStyles(TextStyle(fontSize = mainViewModel.h4TextSize.sp))
    val redTextStyle = AppTextStyles(
        TextStyle(
            color = targetColors.primary,
            fontSize = mainViewModel.h4TextSize.sp,
            fontFamily = FontFamily(
                Typeface.createFromAsset(
                    LocalContext.current.assets,
                    "font/huakang_baby.TTF"
                )
            )
        )
    )
    val yellowTextStyle = AppTextStyles(
        TextStyle(
            color = targetColors.primary,
            fontSize = mainViewModel.h4TextSize.sp,
            fontFamily = FontFamily(
                Typeface.createFromAsset(
                    LocalContext.current.assets,
                    "font/fangzheng_cartoon_jianti.ttf"
                )
            )
        )
    )
    val blueTextStyle = AppTextStyles(
        TextStyle(
            color = targetColors.primary,
            fontSize = mainViewModel.h4TextSize.sp,
            fontFamily = FontFamily(
                Typeface.createFromAsset(
                    LocalContext.current.assets,
                    "font/fangzheng_variegated_carp.TTF"
                )
            )
        )
    )
    mainViewModel.h4TextStyle = when (mainViewModel.chosenThemeId.value) {
        ThemeKinds.RED.name -> {
            redTextStyle.h4
        }
        ThemeKinds.YELLOW.name -> {
            yellowTextStyle.h4
        }
        ThemeKinds.BLUE.name -> {
            blueTextStyle.h4
        }
        else -> {
            defaultTextStyle.h4
        }
    }

    /**
     * 自定义主题 medium Shape 的圆角大小
     */
    val targetShapeSizes = when (mainViewModel.chosenThemeId.value) {
        ThemeKinds.RED.name -> {
            RedShapeSizes
        }
        ThemeKinds.YELLOW.name -> {
            YellowShapeSizes
        }
        ThemeKinds.BLUE.name -> {
            BlueShapeSizes
        }
        else -> {
            DefaultShapeSizes
        }
    }
    mainViewModel.mediumShapeTopStart =
        animateDpAsState(targetShapeSizes.mediumTopStart, TweenSpec(500)).value
    mainViewModel.mediumShapeTopEnd =
        animateDpAsState(targetShapeSizes.mediumTopEnd, TweenSpec(500)).value
    mainViewModel.mediumShapeBottomStart =
        animateDpAsState(targetShapeSizes.mediumBottomStart, TweenSpec(500)).value
    mainViewModel.mediumShapeBottomEnd =
        animateDpAsState(targetShapeSizes.mediumBottomEnd, TweenSpec(500)).value

    MaterialTheme {
        content()
    }
}

//红色主题色值
private val RedThemeColors = AppColors(
    primary = Color(0xFFFF4040),
    background = Color(0x66FF4040)
)

//黄色主题色值
private val YellowThemeColors = AppColors(
    primary = Color(0xFFDAA520),
    background = Color(0x66FFD700)
)

//蓝色主题色值
private val BlueThemeColors = AppColors(
    primary = Color(0xFF436EEE),
    background = Color(0x6600FFFF)
)

//暗夜模式主题色值
private val DarkColors = AppColors(
    primary = Color.White,
    background = Color.Black,
)

//默认主题色值
private val DefaultColors = AppColors(
    primary = Color.Black,
    background = Color.White,
)

//默认主题文案大小
private val DefaultTextSizes = AppTextSizes(40f)
//红色主题文案大小
private val RedTextSizes = AppTextSizes(36f)
//黄色主题文案大小
private val YellowTextSizes = AppTextSizes(30f)
//蓝色主题文案大小
private val BlueTextSizes = AppTextSizes(24f)

//默认主题形状圆角大小
private val DefaultShapeSizes = AppShapeSizes(10.dp, 10.dp, 10.dp, 10.dp)
//红色主题形状圆角大小
private val RedShapeSizes = AppShapeSizes(20.dp, 20.dp, 20.dp, 20.dp)
//黄色主题形状圆角大小
private val YellowShapeSizes = AppShapeSizes(0.dp, 0.dp, 50.dp, 0.dp)
//蓝色主题形状圆角大小
private val BlueShapeSizes = AppShapeSizes(0.dp, 50.dp, 0.dp, 50.dp)

